/* 
 * Copyright (c) 2004-2005 Massachusetts Institute of Technology. This code was
 * developed as part of the Haystack (http://haystack.lcs.mit.edu/) research 
 * project at MIT. Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation files (the 
 * "Software"), to deal in the Software without restriction, including without 
 * limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions: 
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software. 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE. 
 */
/*
 * Created on Dec 19, 2004
 *
 */
package edu.mit.csail.relo.jdt.builder;

import org.apache.log4j.Logger;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import edu.mit.csail.relo.intro.ReloJavaConnectionUtils;
import edu.mit.csail.relo.jdt.ReloJDTPlugin;

/**
 * @author vineet
 *
 */
public class ReloJDTConnectorAction implements IObjectActionDelegate {
    static final Logger logger = ReloJDTPlugin.getLogger(ReloJDTConnectorAction.class);

	protected IWorkbenchPart part;

	/* (non-Javadoc)
     * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart)
     */
    public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		part = targetPart;
    }

    
	protected IJavaProject project;

	/* (non-Javadoc)
     * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
     */
    public void selectionChanged(IAction action, ISelection selection) {
		if (selection instanceof IStructuredSelection) {
			Object obj = ((IStructuredSelection) selection).getFirstElement();
			if (obj instanceof IJavaProject) {
				project = (IJavaProject) obj;
				return;
			}
		}
		
		// else
		project = null;
    }

    
    /* (non-Javadoc)
     * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
     */
    public void run(IAction action) {
        //toggleStateRun();
        confirmAndRun();
    }
    
    protected void toggleStateRun() {
        try {
            // just disconnect (if connected) / reconnect (if disconnected)
	        if (ReloJavaConnectionUtils.connected(project.getProject())) {
	            // already connected to nature
                logger.info("Disconnecting nature...");
                ReloJavaConnectionUtils.disconnect(project.getProject());
	        } else  {
	            // not connected to nature
                logger.info("Connecting nature...");
                ReloJavaConnectionUtils.connect(project.getProject());
	        }

        } catch (CoreException ce) {
            logger.error("Unexpected exception", ce);
        }
    }

    protected void confirmAndRun() {
        try {
	        if (ReloJavaConnectionUtils.connected(project.getProject())) {
	            // already connected to nature
	            boolean disconnect = MessageDialog.openQuestion(
	                    part.getSite().getShell(), 
	                    "Relo Java Builder",
                        "The builder is currently connected to the project. Disconnect?");
	            
	            if (disconnect) ReloJavaConnectionUtils.disconnect(project.getProject());
	        } else {
	            // not connected to nature
	            boolean connect = MessageDialog.openQuestion(
	                    part.getSite().getShell(), 
	                    "Relo Java Builder",
                        "The builder is not currently connected to the project. Connect?");

	            if (connect) ReloJavaConnectionUtils.connect(project.getProject());
	        }
	
        } catch (CoreException ce) {
            logger.error("Unexpected exception", ce);
        }
    }



}
